home *** CD-ROM | disk | FTP | other *** search
- /*
- ** Implementation Module: mstack
- ** Author: Tony DeRose
- ** Last Modified: 22 May 1985
- ** Purpose: matrix stack manipulation.
- */
- #include <stdio.h>
- #include "4D.h"
- #include "mstack.h"
- #include "fast.c"
-
- /* Imports */
- extern char *malloc();
-
- /* Implementation of MStacks */
- typedef struct {
- MstackDisipline disipline;
- int top;
- TransformType4D S[MSTACKSIZE];
- } MstackType;
-
- /*
- ** Create, initialize, and return a matrix stack of the
- ** specified disipline.
- */
- Mstack MsCreate(d)
- MstackDisipline d;
- {
- MstackType *S = (MstackType *) malloc(sizeof(MstackType));
-
- S->disipline = d;
- S->top = -1;
- return (Mstack) S;
- }
-
-
- /*
- ** Push Top(S)*T or T*Top(S) onto Mstack S.
- */
- void MsPush( S, T)
- Mstack S;
- TransformType4D T;
- {
- MstackType *s = (MstackType *) S;
- TransformType4D NewTop, Stop;
-
- if (s->top == (MSTACKSIZE-1)) {
- perror("Matrix stack overflow\n");
- exit(-1);
- }
-
- if (s->top == -1) {
- /* Do a simple push onto the already empty stack s */
- NewTop = T;
- } else {
- /* Either pre or post multiply */
- Stop = s->S[s->top];
- if (s->disipline == Premultiply) {
- TxT4Dm(NewTop,T,Stop);
- } else {
- TxT4Dm(NewTop,Stop,T);
- }
- }
- s->top++;
- s->S[s->top] = NewTop;
- }
-
-
-
- void MsPop(S)
- Mstack S;
- {
- MstackType *s = (MstackType *) S;
-
- if (s->top == -1) {
- /* Can't pop an empty stack */
- perror("Matrix stack underflow\n");
- exit(-1);
- }
- s->top--;
- }
-
- TransformType4D MsTop(S)
- Mstack S;
- {
- MstackType *s = (MstackType *) S;
-
- if (s->top == -1) {
- perror("Can't take top of an empty stack\n");
- }
- return s->S[s->top];
- }
-
- /*
- ** Return the point P transformed by the top of the stack S.
- */
- PointType4D PxTop(P,S)
- PointType4D P;
- Mstack S;
- {
- MstackType *s = (MstackType *) S;
-
- return PxT4D(P,s->S[s->top]);
- }
-
- /*
- ** Return the ray R transformed by the top of S.
- */
- /*Ray RayxTop(R,S)
- Ray R;
- Mstack S;
- {
- MstackType *s = (MstackType *) S;
-
- return RayxT4D(R,s->S[s->top]);
- }
- */
- void MsPrint(f,S)
- FILE *f;
- Mstack S;
- {
- MstackType *s = (MstackType *) S;
- int i;
-
- for (i = s->top; i>-1; i--) {
- PrintTransform4D(f,s->S[i]);
- fprintf(f,"---------------------------\n");
- }
- }
-
-
-